// pages/home/home.js
var {
    getHomeBanner,
    getHomeGoods
} = require("../../api/Home");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgs: [
            // "/images/banner01.jpg",
            // "/images/banner02.jpg",
            // "/images/banner03.jpg",
            // "/images/banner04.jpg",
        ],
        iconArray: [{
                url: "/images/icon-qiandao.png",
                text: "大家电",
            },
            {
                url: "/images/icon-fujin.png",
                text: "热门推荐",
            },
            {
                url: "/images/icon-zhanhui.png",
                text: "海外购",
            },
            {
                url: "/images/icon-fuli.png",
                text: "苏宁房产",
            },
            {
                url: "/images/icon-muma.png",
                text: "手机相机",
            },
            {
                url: "/images/icon-tiyu.png",
                text: "电脑办公",
            },
            {
                url: "/images/icon-xingxing.png",
                text: "厨卫电器",
            },
            {
                url: "/images/icon-qinzi.png",
                text: "食品酒水",
            },
        ],
        goodsList: [
            // {
            //     id:1,
            //     goodsImage:"/images/lists01.webp",
            //     goodsName:"1商品名称1商品名称1商品名称1商品名称1商品名称",
            //     goodsAddress:"广州",
            //     goodsPrice:"200",
            //     originalPrice:"300",
            // },
            // {
            //     id:2,
            //     goodsImage:"/images/lists02.webp",
            //     goodsName:"2商品名称",
            //     goodsAddress:"广州",
            //     goodsPrice:"200",
            //     originalPrice:"300",
            // },
            // {
            //     id:3,
            //     goodsImage:"/images/1.jpg",
            //     goodsName:"2商品名称",
            //     goodsAddress:"广州",
            //     goodsPrice:"200",
            //     originalPrice:"300",
            // },
            // {
            //     id:4,
            //     goodsImage:"/images/lists04.webp",
            //     goodsName:"2商品名称",
            //     goodsAddress:"广州",
            //     goodsPrice:"200",
            //     originalPrice:"300",
            // },
        ]

    },
    goCategory(options) {
        let index = options.currentTarget.dataset.index;
        wx.setStorageSync('categoryIndex', index);
        wx.switchTab({
            url: '/pages/category/category',
        })
    },
    // 添加到购物车
    addShopping(options) {
        let id = options.target.dataset.id;
        let index = this.data.goodsList.findIndex(item => item.id == id);
        // console.log(index);
        let cartLists = {
            id: this.data.goodsList[index].id,
            goodsImage: this.data.goodsList[index].goodsImage,
            goodsName: this.data.goodsList[index].goodsName,
            goodsPrice: this.data.goodsList[index].goodsPrice,
            num: 1,
        };
        let goodsCarList = wx.getStorageSync('goodsCarList');
        if (goodsCarList) {
            let i = goodsCarList.findIndex(item => item.id == id);
            if (i != -1) {
                goodsCarList[i].num += 1;
            } else {
                goodsCarList.push(cartLists);
            }
            wx.setStorageSync('goodsCarList', goodsCarList);
        } else {
            wx.setStorageSync('goodsCarList', [cartLists])
        }
        wx.showToast({
            title: "已添加到购物车",
            duration:2000
          })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        getHomeBanner().then((data) => {
            this.setData({
                imgs: data.message
            })
        })
        getHomeGoods().then(data => {
            let lists = [];
            let goodslists = data.message.goods;
            goodslists.forEach(item => {
                let {
                    goods_id,
                    goods_small_logo,
                    goods_name,
                    goods_price
                } = item;
                let data = {
                    id: goods_id,
                    goodsName: goods_name,
                    goodsPrice: goods_price,
                    goodsImage: goods_small_logo,
                    originalPrice: goods_price + 100,
                };
                if (goods_small_logo) {
                    lists.push(data);
                }
            })
            this.setData({
                goodsList: lists
            })
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})